package com.woniuxy.microgoodsserver.brandmag.mapper;

import com.woniuxy.microcommonserver.bean.gy.BrandInfo;
import com.woniuxy.microcommonserver.bean.gy.GoodCategoryInfo2;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**操作品牌库的接口

 * @params:
 * @return
 */
@Mapper
public interface BrandMapper {
    /**
        新增品牌
     * @params:
     * @return
     */
    @Insert("insert into brand_info(brand_name,brand_shorter,official_website,careful_brand," +
            "disable,sort,brand_image,brand_story,status,brand_type) values(#{b.brandName},#{b.brandShorter},#{b.officialWebsite}," +
            "#{b.carefulBrand},#{b.disable},#{b.sort},#{b.brandImage},#{b.brandStory},0,#{b.brandType})")
    public void addBrand(@Param("b") BrandInfo brandInfo);

    /**
        根据品牌名称查询
     * @params:
     * @return
     */
    @ResultType(BrandInfo.class)
    @Select("<script>" +
            "select id from brand_info where brand_name = #{b.brandName} " +
            "<if test='b.brandType !=null and b.brandType !=\"\" '> " +
            "and brand_type = #{b.brandType} " +
            "</if>" +
            "</script>")
    public BrandInfo findByName(@Param("b") BrandInfo brandInfo);


    /**
        添加中间表
     * @params:
     * @return
     */
    @Insert("insert into category_brand(good_category_info2_id,brand_info_id) " +
            "values(#{id},#{id1})")
    public void addSortAndBran(@Param("id") Integer id,@Param("id1") Integer id1);

    /**查询所有的品牌

     * @params:
     * @return
     */
    @ResultType(BrandInfo.class)
    @Select("select id,brand_name brandName,brand_type brandType,official_website officialWebsite," +
            "sort,careful_brand carefulBrand,disable from brand_info where status=0")
    public List<BrandInfo> findBrand();

    /**
        删除品牌类型
     * @params:
     * @return
     */
    @Update("update brand_info set status=1 where id = #{b.id}")
    public void deleteBrand(@Param("b") BrandInfo brandInfo);

    /**
     根据ID查询品牌信息
     * @params:
     * @return
     */
    @Select("select id,brand_name brandName,brand_shorter brandShorter,official_website officialWebsite," +
            "careful_brand carefulBrand,disable,sort,brand_image brandImage,brand_story brandStory," +
            "brand_type brandType from brand_info where id = #{b.id} and status=0")
    public BrandInfo findById(@Param("b")BrandInfo brandInfo);

    /**
        修改品牌信息
     * @return
     */
    public void updateBrand(@Param("b") BrandInfo brandInfo);


    /**
       修改中间表，品牌与子目的对应关系
     * @params:
     * @return
     */
    @Update("update category_brand set good_category_info2_id = #{g.id} where brand_info_id = #{b.id} and good_category_info2_id = #{oldSonId}")
    public void updateBrandAndSort(@Param("b") BrandInfo brandInfo, @Param("g")GoodCategoryInfo2 goodCategoryInfo2,@Param("oldSonId")int oldSonId);

    /**
     根据子目ID查询下面所有品牌
     * @params:
     * @return
     */
    @Select("select bi.id id,bi.brand_shorter brandShorter from category_brand cb,brand_info bi where cb.good_category_info2_id = #{g.id} and cb.brand_info_id = bi.id and bi.status=0")
    public List<BrandInfo> findBrandBySort(@Param("g") GoodCategoryInfo2 goodCategoryInfo2);
}
